package com.lin.blog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.blog.entity.Permission;
import com.lin.blog.mapper.PermissionMapper;
import com.lin.blog.service.IPermissionService;
import com.lin.constant.ResultCode;
import com.lin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;


    /**
     * 通过角色名获取权限
     * @param roleName
     * @return
     */
    @Override
    public List<Permission> getPermissionByRoleName(String roleName){
        return permissionMapper.getPermissionByRoleName(roleName);
    }

    /**
     * 分页获取 tPermissionList
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result listPermission(Long pageNum, Long pageSize){
        Page page = new Page(pageNum, pageSize);
        return Result.success(permissionMapper.selectPage(page,null));
    }



    /**
     * 获取 PermissionList
     * @return
     */
    @Override
    public Result listPermission(){
        return Result.success(permissionMapper.selectList(null));
    }

    /**
     * 通过主键ID查询Permission
     *
     * @param id
     * @return
     */
    @Override
    public Result getPermission(Long id) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }

        // 查询
        Permission permission = permissionMapper.selectById(id);

        // 不存在
        if (permission == null) {
            return Result.failure(ResultCode.TAGS_NOT_EXIT);
        }

        return Result.success(permission);
    }

    /**
     * 插入一条Permission记录
     * @param permission
     * @return
     */
    @Override
    public Result savePermission(Permission permission) {

        permission.setCreateTime(LocalDateTime.now());

        if (permissionMapper.insert(permission) > 0){

            return Result.success(permission);
        }
        return Result.failure(ResultCode.INSERT_ERROR);
    }

    /**
     * 通过主键ID删除Permission
     * @param id
     * @return
     */
    @Override
    public Result removePermission(Long id) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (permissionMapper.deleteById(id) > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.DELETE_ERROR);
    }


    /**
     * 通过主键ID更新Permission
     * @param id
     * @param permission
     * @return
     */
    @Override
    public Result updateTags(Long id, Permission permission) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        permission.setPermissionId(id);

        if (permissionMapper.updateById(permission) > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.UPDATE_ERROR);
    }
}
